package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Compensation;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.CompensationMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper mapper;
    @Autowired
    private CompensationMapper compensationMapper;

    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public void insert(Employee record) {
        //对密码进行加密操作
        Md5Hash md5Hash = new Md5Hash(record.getPassword(), record.getUsername(), 2);
        record.setPassword(md5Hash.toString());
        mapper.insert(record);
        //处理中间表
        List<Role> roles = record.getRoles();
        for (Role role : roles) {
            mapper.insertRoleRelation(record.getId(),role.getId());
        }

        //关联薪资
        Compensation compensation = new Compensation();
        compensation.setEmployee(record);
        compensation.setDept(record.getDept());
        compensation.setStatus(true);
        compensationMapper.insertEmpAndDept(compensation);
    }

    public Employee selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Employee> selectAll(EmployeeQueryObject qo) {
        return mapper.selectAll(qo);
    }

    public void updateByPrimaryKey(Employee record) {
        mapper.updateByPrimaryKey(record);
        mapper.deleteRoleRelation(record.getId());
        List<Role> roles = record.getRoles();
        for (Role role : roles) {
            mapper.insertRoleRelation(record.getId(),role.getId());
        }

        //关联薪资
        Compensation compensation = compensationMapper.selectByEmployee(record.getId());
        compensation.setEmployee(record);
        compensation.setDept(record.getDept());
        compensation.setStatus(true);
        compensationMapper.updateEmpAndDept(compensation);
    }

    public PageResult query(EmployeeQueryObject qo) {
        int count = mapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, mapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    public Employee getByUsername(String username) {
        return mapper.getByUsername(username);
    }

    public void changeState(Long id) {
        mapper.changeState(id);
    }

    public void changePassword(Long id) {
        mapper.changePassword(id);
    }

    public List<Long> getRoleIdByEmpId(Long employeeId) {
        return mapper.getRoleIdByEmpId(employeeId);
    }

    public List<Employee> selectlist() {
        return mapper.selectlist();
    }

    @Override
    public List<Employee> selectAll() {
        return mapper.selectAll();
    }
}
